#!/bin/bash
SCRIPT_DIR=$(cd "$(dirname "$0")" && pwd)
COMMON_SHELL="$SCRIPT_DIR/../common.sh" 
SFTP_TOOL="$SCRIPT_DIR/../tools/sftp"

if [ ! -f "$COMMON_SHELL" ]; then
    if [ ! -f ./common.sh ]; then
        wget https://gitee.com/anolis/hygon-devkit/raw/master/common.sh
        chmod +x common.sh
    fi
    source ./common.sh
else
    source $COMMON_SHELL
fi

PKG_SUFFIX=$(get_os_info | sed -n '1p')
OS_TYPE=$(get_os_info | sed -n '2p')
OS_VERSION_ID=$(get_os_info | tail -n 1)

# Check if the correct number of arguments is provided
if [ "$#" -ne 1 ]; then
    echo "Usage: $0 <version>"
    exit 1
fi

VERSION="$1"
case "$VERSION" in
    1.0.0*)
        SDF_VERSION=""
        ;;
    2.0.0*)
        SDF_VERSION=""
        ;;
    3.2.0*)
        SDF_VERSION=2.2.0-20240530
        ;;
    3.3.0*)
        SDF_VERSION=2.3.0-20250130
        ;;
    *)
        echo "Unkown version：$VERSION" >&2
        exit 1
        ;;
esac

LOCAL_DIR=${SCRIPT_DIR}/pkg/tkm-$1
mkdir -p "$LOCAL_DIR"
# Get the file name at remote path
if [ ! -f "$SFTP_TOOL" ]; then
    if [ ! -f ./sftp ]; then
        wget https://gitee.com/anolis/hygon-devkit/raw/master/tools/sftp
    fi
    chmod +x sftp
    FILE_NAME=$(./sftp list "/hygon-devkit/4.0/2025-01-01/tkm/tkm-$1" | grep -oP "hygon-psp-.*?\.\Q$PKG_SUFFIX\E")
else
    FILE_NAME=$($SFTP_TOOL list "/hygon-devkit/4.0/2025-01-01/tkm/tkm-$1" | grep -oP "hygon-psp-.*?\.\Q$PKG_SUFFIX\E")
fi

echo ""$LOCAL_DIR/$FILE_NAME""
# If the directory don't have the file, download it
if [ ! -f "$LOCAL_DIR/$FILE_NAME" ]; then
    if [ ! -f "$SFTP_TOOL" ]; then
        ./sftp download /hygon-devkit/4.0/2025-01-01/tkm/tkm-$1/$FILE_NAME $LOCAL_DIR/$FILE_NAME
    else
        echo "$SFTP_TOOL download /hygon-devkit/4.0/2025-01-01/tkm/tkm-$1/$FILE_NAME $LOCAL_DIR/$FILE_NAME"
        $SFTP_TOOL download /hygon-devkit/4.0/2025-01-01/tkm/tkm-$1/$FILE_NAME $LOCAL_DIR/$FILE_NAME
    fi
fi

if [ $SDF_VERSION ]; then
    if [ ! -f "$SFTP_TOOL" ]; then
        SDF_FILE_NAME=$(./sftp list "/hygon-devkit/4.0/2025-01-01/tkm/sdf-$SDF_VERSION" | grep -oP "hygon-sdf-.*?\.\Q$PKG_SUFFIX\E")
    else
        SDF_FILE_NAME=$(${SFTP_TOOL} list "/hygon-devkit/4.0/2025-01-01/tkm/sdf-$SDF_VERSION" | grep -oP "hygon-sdf-.*?\.\Q$PKG_SUFFIX\E")
    fi
    if [ -z "$(ls -A pkg/sdf-$SDF_VERSION/$SDF_FILE_NAME 2>/dev/null)" ] ; then
        mkdir -p ${SCRIPT_DIR}/pkg/sdf-$SDF_VERSION
        if [ ! -f "$SFTP_TOOL" ]; then
            ./sftp download /hygon-devkit/4.0/2025-01-01/tkm/sdf-$SDF_VERSION/$SDF_FILE_NAME ${SCRIPT_DIR}/pkg/sdf-$SDF_VERSION/$SDF_FILE_NAME
        else
            ${SFTP_TOOL} download /hygon-devkit/4.0/2025-01-01/tkm/sdf-$SDF_VERSION/$SDF_FILE_NAME ${SCRIPT_DIR}/pkg/sdf-$SDF_VERSION/$SDF_FILE_NAME
        fi
    fi
fi

# Detect the package type, use the dpkg -i or rpm -ivh command to install the package
if [ "$PKG_SUFFIX" == "deb" ]; then
    dpkg -i $LOCAL_DIR/$FILE_NAME
    if [ $SDF_VERSION ]; then
        dpkg -i ${SCRIPT_DIR}/pkg/sdf-$SDF_VERSION/$SDF_FILE_NAME
    fi
else
    rpm -ivh $LOCAL_DIR/$FILE_NAME
    if [ $SDF_VERSION ]; then
        rpm -ivh ${SCRIPT_DIR}/pkg/sdf-$SDF_VERSION/$SDF_FILE_NAME
    fi
fi
